package com.ts.api.module.api.service.impl;

import com.gitee.apanlh.util.valid.Assert;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.common.constant.api.ApiBizTypeEnum;
import com.ts.api.common.constant.api.ApiEnum;
import com.ts.api.common.constant.api.ApiVersionModeEnum;
import com.ts.api.exp.ApiMsgException;
import com.ts.api.module.api.convert.receive.entity.ApiReceiveApply;
import com.ts.api.module.api.convert.receive.entity.ApiReceivePhoneMd5DataExchange;
import com.ts.api.module.api.convert.receive.handler.DelegatingApiReceivePhoneMd5ConvertHandler;
import com.ts.api.module.api.entity.ApiUserRequest;
import com.ts.api.module.api.entity.bo.ApiBizMatchBO;
import com.ts.api.module.api.entity.bo.ApiDataExchangeBO;
import com.ts.api.module.api.entity.bo.ApiParameterBO;
import com.ts.api.module.api.entity.bo.ApiPreRequiredBO;
import com.ts.api.module.api.entity.bo.ApiRequiredFlowBO;
import com.ts.api.module.api.entity.po.ApiKeyPO;
import com.ts.api.module.api.entity.vo.ReqApiParameterVO;
import com.ts.api.module.api.handler.ApiReceiveHandler;
import com.ts.api.module.api.service.ApiBizApplyService;
import com.ts.api.module.api.service.ApiBizDataExchangeService;
import com.ts.api.module.api.service.ApiBizRequiredService;
import com.ts.api.module.api.service.ApiReceiveSpecialService;
import com.ts.api.module.product.entity.po.ProductApplyFlowPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class ApiReceiveSpecialServiceImpl implements ApiReceiveSpecialService {

    @Autowired
    private ApiReceiveHandler apiReceiveHandler;
    @Autowired
    private DelegatingApiReceivePhoneMd5ConvertHandler delegatingApiReceivePhoneMd5ConvertHandler;
    @Autowired
    private ApiBizDataExchangeService apiBizDataExchangeService;
    @Autowired
    private ApiBizRequiredService apiBizRequiredService;
    @Autowired
    private ApiBizApplyService apiBizApplyService;

    @Override
    public Map<String, Object> dataExchange(ApiKeyPO apiKeyPO, RequestVO requestVO) {
        try {
            //  1.解析撞库请求实体
            ReqApiParameterVO reqApiParameterVO = apiReceiveHandler.handleDataExchangeRequest(apiKeyPO, requestVO);

            //  验证版本
            if (!ApiVersionModeEnum.MD5.hasVersionMode(apiKeyPO.getApiVersionMode())) {
                throw new ApiMsgException(ApiEnum.API_VERSION_MODE_ERROR);
            }

            //  2.撞库前置校验-特殊计划匹配模式
            ApiBizMatchBO specialPlan = ApiBizMatchBO.createSpecialPlan(false);
            ApiPreRequiredBO apiPreRequiredBO = ApiPreRequiredBO.create(reqApiParameterVO, ApiBizTypeEnum.DATA_EXCHANGE, null, specialPlan);
            ApiParameterBO apiParameterBO = apiBizRequiredService.preRequiredParam(apiPreRequiredBO);

            //  3.执行撞库
            ApiDataExchangeBO apiDataExchangeBO = apiBizDataExchangeService.dataExchange(apiParameterBO);

            //  4.返回撞库
            ApiReceivePhoneMd5DataExchange message = ApiReceivePhoneMd5DataExchange.create(apiDataExchangeBO, apiKeyPO);
            return delegatingApiReceivePhoneMd5ConvertHandler.respDataExchange(message);
        } catch (Exception e) {
            throw apiReceiveHandler.dataExchangeWrapApiException(e);
        }
    }

    @Override
    public Map<String, Object> apply(ApiKeyPO apiKeyPO, RequestVO requestVO) {
        try {
            //  1.解析进件请求实体
            ReqApiParameterVO reqApiParameterVO = apiReceiveHandler.handleApplyRequest(apiKeyPO, requestVO);

            //  验证版本
            if (!ApiVersionModeEnum.MD5.hasVersionMode(apiKeyPO.getApiVersionMode())) {
                throw new ApiMsgException(ApiEnum.API_VERSION_MODE_ERROR);
            }

            //  2.获取参数
            ApiUserRequest apiUserRequest = reqApiParameterVO.getApiUserRequest();
            String phoneMd5 = apiUserRequest.getPhoneMd5();

            //  3.查询是否渠道存在该流程
            ApiRequiredFlowBO apiRequiredFlowBO = ApiRequiredFlowBO.createByApiClientId(phoneMd5, apiKeyPO.getApiClientId());
            ProductApplyFlowPO productApplyFlowPO = apiBizRequiredService.requiredSucDataExchangeFlow(apiRequiredFlowBO);
            Assert.isNotNullThrows(productApplyFlowPO, new ApiMsgException(ApiEnum.API_APPLY_FLOW_ERROR));

            //  4.进件前置参数验证-特殊计划类型匹配
            ApiBizMatchBO specialPlanApply = ApiBizMatchBO.createSpecialPlanApply();
            ApiPreRequiredBO apiPreRequiredBO = ApiPreRequiredBO.create(reqApiParameterVO, ApiBizTypeEnum.APPLY, null, specialPlanApply);
            ApiParameterBO apiParameterBO = apiBizRequiredService.preRequiredParam(apiPreRequiredBO);

            //  5.进件流程
            ApiReceiveApply message = apiBizApplyService.apply(apiUserRequest, apiParameterBO, productApplyFlowPO);

            //  6.返回进件流程
            return delegatingApiReceivePhoneMd5ConvertHandler.respApply(message);
        } catch (Exception e) {
            throw apiReceiveHandler.applyWrapApiException(e);
        }
    }
}
